var express = require('express');
var router = express.Router();

var mongoose = require('mongoose')
var Schema = mongoose.Schema;
mongoose.connect('mongodb://127.0.0.1:27017/Unihan', {useNewUrlParser: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});

var lineSchema = new Schema({ //定义数据模型
  _id: String,
  x: Number,
  y: Number,
},{
  collection: 'lineList', // 表名
  versionKey:false
});
var line = mongoose.model('lineList', lineSchema);

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index')
});

var lineQuery = line.find({},{_id: 0, x: 1, y: 1}).limit(100000)
router.get('/get/data', function(req, res, next) {
  lineQuery.exec(function(err,result){
    if(err){
      console.log(err)
    }else{
      var newResult = []
      result.forEach(item => {
        newResult.push([item.x,item.y])
      });
      res.send(newResult)
    }
  })
});

module.exports = router;
